package com.zero.logistics.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zero.logistics.mapper.OrderDetailMapper;
import com.zero.logistics.mapper.OrderMapper;
import com.zero.logistics.pojo.Order;
import com.zero.logistics.pojo.OrderDetail;
import com.zero.logistics.pojo.OrderDetailExample;
import com.zero.logistics.pojo.OrderDetailExample.Criteria;
import com.zero.logistics.pojo.OrderExample;
import com.zero.logistics.service.OrderService;

/*
 *	@date:2020年3月30日 下午7:38:14
 *	@author:Zero
 *	@version:Future
 *	@description: 
 *
 */
@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private OrderDetailMapper orderDetailMapper;

	@Override
	public int deleteByPrimaryKey(Long orderId) {
		//1.先删除订单信息
		int deleteOrderResult = orderMapper.deleteByPrimaryKey(orderId);
		
		//2.再删除订单明细信息
		OrderDetailExample example = new OrderDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(orderId);
		orderDetailMapper.deleteByExample(example);
		
		return deleteOrderResult;  //因为这里已经切入了事务的概念，所以不用担心订单被删除了但是订单详情没有被删除的情况
	}

	@Override
	public int insert(Order order) {
		//1.先新增订单信息
		System.out.println("插入前：" + order.getOrderId());
		int insertOrderResult = orderMapper.insert(order);
		//2.然后获取订单自动生成的主键（即orderId）,这个orderId需要设置到订单明细中
		Long orderId = order.getOrderId();
		System.out.println("插入后：" + order.getOrderId());
		//3.将orderId设置到每个订单明细中，再新增订单明细信息
		List<OrderDetail> orderDetails = order.getOrderDetails();
		for (OrderDetail orderDetail : orderDetails) {
			orderDetail.setOrderId(orderId);
			orderDetailMapper.insert(orderDetail);
		}
		return insertOrderResult;
	}

	@Override
	public List<Order> selectByExample(OrderExample example) {
		return orderMapper.selectByExample(example);
	}

	@Override
	public Order selectByPrimaryKey(Long orderId) {
		return orderMapper.selectByPrimaryKey(orderId);
	}

	@Override
	public int updateByPrimaryKeySelective(Order record) {
		return orderMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int deleteByIds(String[] ids) {
		
		int deleteOrdersResult = orderMapper.deleteByIds(ids);
		
		//同时也将订单明细信息删除
		for (String orderId : ids) {
			OrderDetailExample example = new OrderDetailExample();
			Criteria criteria = example.createCriteria();
			criteria.andOrderIdEqualTo(Long.parseLong(orderId));
			orderDetailMapper.deleteByExample(example );
		}
		
		return deleteOrdersResult;
				
	}

	@Override
	public List<OrderDetail> selectDetailsByOrderId(Long orderId) {
		OrderDetailExample example = new OrderDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(orderId);
		List<OrderDetail> orderDetails = orderDetailMapper.selectByExample(example );
		return orderDetails;
	}

}
